package com.patterns.mediator.SaleComputer;

/**
 * Created by Xiao Yiyun on 2017/4/29.
 * 具体中介者
 */
public class Mediatror extends AbstractMediator {
    public void execute(String str, Object...objects) {
        if (str.equals("purchase,buy")) {
            //采购电脑
            this.buyComputer((Integer)objects[0]);
        } else if (str.equals("sale.offsell")) {
            //销售电脑
            this.sellComputer((Integer)objects[0]);
        } else if(str.equals("stock.clear")) {
            //清仓处理
            this.clearStock();
        }
    }
    //采购电脑
    private void buyComputer(int number) {
        int saleStatus = super.sale.getSalseStatus();
        if (saleStatus > 80) {
            //销售情况良好
            System.out.println("采购电脑" + number + "台");
            super.stock.increase(number);
        } else {
            //销售情况不好
            int BuyNumber = number/2;
            System.out.println("采购电脑" + number + "台");
        }
    }
    //销售电脑
    private void sellComputer(int number) {
        if (super.stock.getStockNumber() < number) {
            //库存数量不够销售
            super.purchase.buyComputer(number);
        }
        super.stock.decrease(number);
    }
    //折价销售电脑
    private void offSell() {
        System.out.println("折价销售电脑" + stock.getStockNumber() + "台");
    }
    //清仓处理
    private void clearStock() {
        //要求清仓销售
        super.sale.offSale();
        //要求采购人员不要采购
        super.purchase.refuseBuyComputer();
    }
}
